package scripts;

import api.GetVerifyApi;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.HttpClientUtils;
import utils.ResponseData;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;

public class Test_Verify {
    private  final GetVerifyApi getVerifyApi=new GetVerifyApi();
    private static final Logger log= LoggerFactory.getLogger(Test_Verify.class);
    @DataProvider
    public Object[][] build_data() throws IOException {
        String json_str= FileUtils.readFileToString(new File("./data/verify.json"),"utf-8");
        JSONArray jsonArray= JSONObject.parseArray(json_str);
        Object[][] test_data=new Object[jsonArray.size()][];
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            Object type=jsonObject.get("type");
            Object status_code=jsonObject.get("statusCode");
            test_data[i]=new Object[]{type,status_code};
        }
        log.info("test_data={}", Arrays.deepToString(test_data));
        return test_data;

    }
    @Test
    public static void test_01() throws IOException, URISyntaxException {
        String data=String.valueOf(new Random().nextDouble());
        log.info("data={}",data);
        ResponseData responseData= GetVerifyApi.verify(data);
        Assert.assertEquals(responseData.getCode(),200);
    }
    @Test
    public static void test_02() throws IOException, URISyntaxException {
        String data=String.valueOf(new Random().nextInt());
        log.info("data={}",data);
        ResponseData responseData= GetVerifyApi.verify(data);
        Assert.assertEquals(responseData.getCode(),200);
    }
    @Test
    public static void test_03() throws IOException, URISyntaxException {
        String data= RandomStringUtils.randomAlphanumeric(3);
        log.info("data={}" ,data);
        ResponseData responseData=GetVerifyApi.verify(data);
        Assert.assertEquals(responseData.getCode(),400);
        System.out.println("hello");
    }
    @Test(dataProvider = "build_data")
    public static void test_04(String type,int statusCode) throws IOException, URISyntaxException {
        String random="";
        if("float".equals(type)){
            random=String.valueOf(new Random().nextDouble());
        }else if("int".equals(type)){
            random=String.valueOf(new Random().nextInt());
        }else if ("char".equals(type)){
            random= UUID.randomUUID().toString();
        }else if("empty".equals(type)){
            random="";
        }
        ResponseData responseData= GetVerifyApi.verify(random);
        Assert.assertEquals(responseData.getCode(),statusCode);
    }

}
